package com.bridgeintelligent.tag.customer.service;

import com.bridgeintelligent.tag.customer.entity.AnalysisSetup;
import com.bridgeintelligent.tag.customer.entity.AnalysisTemplate;
import com.bridgeintelligent.tag.customer.entity.Result;
import com.bridgeintelligent.tag.customer.entity.TemplatePage;
import com.bridgeintelligent.tag.customer.mapper.AnalysisSetupMapper;
import com.bridgeintelligent.tag.customer.mapper.AnalysisTemplateMapper;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.DateHelper;
import com.bridgeintelligent.tag.webserver.security.TagSecurityHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wayneleo.quickstart.framework.IDGenerator;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板服务层接口实现
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
@Slf4j
public class AnalysisTemplateServiceImpl implements IAnalysisTemplateService {

    private AnalysisTemplateMapper templateMapper;

    private AnalysisSetupMapper analysisSetupMapper;

    private IAnalysisSetupService setupService;

    /**
     * 新增模板
     * @param template
     */
    @Override
    public void addAnalysisTemplate(AnalysisTemplate template) {
        User user = TagSecurityHelper.currentUser();
        template.setId(IDGenerator.gen())
                .setCtTime(DateHelper.currentDateTime(DateHelper.PATTERN_DATE_1))
                .setUtTime(DateHelper.currentDateTime(DateHelper.PATTERN_DATE_1))
                .setCtName(user.getUserId());
        //保存基础信息
        templateMapper.addAnalysisTemplate(template);
        //保存分析设置信息
        List<AnalysisSetup> setups = template.getSetups();
        if (!CollectionUtils.isEmpty(setups)){
            setups.forEach(e ->{
                e.setId(IDGenerator.gen());
                e.setTpId(template.getId());
                analysisSetupMapper.addAnalysisSetup(e);
            });
        }

    }


    /**
     * 校验模板名是否重复
     * @param id
     * @param tlName
     * @return
     */
    @Override
    public boolean checkTlName(String id,String tlName) {
        String name = templateMapper.checkName(tlName);
        if (StringUtils.isEmpty(name)){
            return false;
        }else{
            if (!StringUtils.isEmpty(id) && StringUtils.equals(name,id)){
                return false;
            }
            return true;
        }
    }

    /**
     * 修改模板
     * @param template
     */
    @Override
    public void updateAnalysisTemplate(AnalysisTemplate template) {
        template.setUtTime(DateHelper.currentDateTime(DateHelper.PATTERN_DATE_1));
        //修改模板信息
        templateMapper.updateAnalysisTemplate(template);
        setupService.delByTpId(template.getId());
        //修改分析设置信息
        List<AnalysisSetup> setups = template.getSetups();
        if (!CollectionUtils.isEmpty(setups)){
            setups.forEach(e ->{
                e.setId(IDGenerator.gen());
                e.setTpId(template.getId());
                analysisSetupMapper.addAnalysisSetup(e);
            });
        }

    }


    /**
     * 删除模板
     * @param id
     */
    @Override
    public void delAnalysisTemplate(String id) {
        templateMapper.delAnalysisTemplate(id);
        analysisSetupMapper.delSetupByTpId(id);
    }

    /**
     * 模板详情
     * @param id
     * @return
     */
    @Override
    public AnalysisTemplate findAnalysisTemplate(String id) {
        AnalysisTemplate template = templateMapper.findAnalysisTemplate(id);
        List<AnalysisSetup> setups = analysisSetupMapper.findByTpId(id);
        template.setSetups(setups);
        return template;
    }


    /**
     * 分页模板列表
     * @param pageNumber
     * @param pageSize
     * @param tlName
     * @param stTime
     * @param edTime
     * @return
     */
    @Override
    public Result pageList(Integer pageNumber, Integer pageSize, String tlName,String stTime, String edTime,String indexId) {

        pageNumber = pageNumber == null ? 1 : pageNumber;
        pageSize = pageSize == null ? 10 : pageSize;
        //获取当前用户的id
        User user = TagSecurityHelper.currentUser();
        Map map = new HashMap();
        map.put("tlName", tlName);
        map.put("stTime", stTime);
        map.put("edTime", edTime);
        map.put("indexId",indexId);
        Page page = PageHelper.startPage(pageNumber, pageSize);
        List<AnalysisTemplate> list = templateMapper.findList(map);
        if (!CollectionUtils.isEmpty(list)){
            list.forEach(e -> {
                List<AnalysisSetup> setups = analysisSetupMapper.findByTpId(e.getId());
                e.setSetups(setups);
            });
        }
        TemplatePage<AnalysisTemplate> templatePage = new TemplatePage<>();
        templatePage.setContent(list);
        templatePage.setTotalPages(page.getPages());
        templatePage.setTotalElements(page.getTotal());
        templatePage.setSize(page.getPageSize());
        templatePage.setRequestPager(pageNumber);
        return new Result(0, "查询成功",templatePage);
    }

}
